<?php

namespace App\Http\Controllers\Home\Search;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class SearchController extends Controller
{
    //展示分类搜索页面
    public function index($cat_id,$paixu)
    {
    	
    	//查询几个热搜榜
    	$hot = DB::table('goods')
    			-> limit(4)
    			-> get();
    	//所传为分类id,根据该分类id取出所有该分类下的商品,考虑扩展分类和商品的主分类

    	//取出扩展分类中的商品id
    	$goods_id = DB::table('goods_cat')
    			-> select('goods_id')
    			-> where('cat_id',$cat_id)
    			-> get();
        // dd($goods_id);
    	//将主分类的商品先放进数组中
    	$goods = DB::table('goods')
                -> select('id')
    			-> where('cat_id',$cat_id)
    			-> get();
        // dd($goods);
    	// dd($goods);
    	//定义一个数组放商品属性
    	$arr = array();
    	foreach($goods as $v)
    	{
    		$arr[] = $v -> id;
    	}

    	//将扩展分类的属性放进数组
    	foreach($goods_id as $v)
    	{
    		$arr[] = $v -> goods_id;
    	}

        if($paixu == 1)
        {
            $arr = DB::table('goods')
                -> whereIn('id',$arr)
                -> where('is_on_sale',1)
                -> paginate(4);
        }elseif($paixu == 2)
        {
            $arr = DB::table('goods')
                -> whereIn('id',$arr)
                -> where('is_on_sale',1)
                -> orderBy('goods_price','desc')
                -> paginate(4);
        }elseif($paixu == 3)
        {
             $arr = DB::table('goods')
                -> whereIn('id',$arr)
                -> where('is_on_sale',1)
                -> orderBy('goods_price')
                -> paginate(4);           
        }



    	//根据分类id获取筛选条件
    	/* ****************** 系列值************* */
    		


    	/* ****************** 价格************* */
    		//分的段数
    		// $sectionCount = 6;
    		//取出最大价格与最小价格
    		// $arrid = array();
    		// foreach($arr as $v)
    		// {
    		// 	$arrid[] = $v -> id;
    		// }
    		// $maxprice = DB::table('goods')
    		// 		-> select(DB::raw("Max(goods_price)as Maxprice"))
    		// 		-> whereIn('id',$arrid)
    		// 		-> get();
    		// dd($maxprice);
        //取出1级分类
        $cateone = DB::table('category')
                -> where('pid',0)
                -> get();
        //取出二级分类
        $catetwo = array();

        foreach($cateone as $v)
        {
            $catetwo[] = DB::table('category')
                        -> where('pid',$v -> id)
                        -> get();
        }

        // dd($catetwo);
        //取出三级分类
        $catethree = array();
        foreach( $catetwo as $k => $v)
        {
            foreach($v as $k1 => $v1)
            {
                $catethree[] = DB::table('category')
                            -> where('pid', $v1 -> id)
                            -> get();
            }
        }

    	return view('home.search.search',['arr' => $arr,'hot' => $hot,'cat_id' => $cat_id,'cateone' => $cateone,'catetwo' => $catetwo,'catethree' => $catethree]);
    }

    public function search(Request $request,$paixu)
    {
        // dd($request -> series);
        if($paixu == 1)
        {
            $data = DB::table('goods')
                -> where('series','like','%' . $request -> series . '%')
                -> where('is_on_sale',1)
                -> paginate(3); 
        }elseif($paixu == 2)
        {
            $data = DB::table('goods')
                -> where('series','like','%' . $request -> series . '%')
                -> where('is_on_sale',1)
                -> orderBy('goods_price','desc')
                -> paginate(3);            
        }elseif($paixu == 3)
        {
            $data = DB::table('goods')
                -> where('series','like','%' . $request -> series . '%')
                -> where('is_on_sale',1)
                -> orderBy('goods_price')
                -> paginate(3);            
        }
        //取出1级分类
        $cateone = DB::table('category')
                -> where('pid',0)
                -> get();
        //取出二级分类
        $catetwo = array();

        foreach($cateone as $v)
        {
            $catetwo[] = DB::table('category')
                        -> where('pid',$v -> id)
                        -> get();
        }

        // dd($catetwo);
        //取出三级分类
        $catethree = array();
        foreach( $catetwo as $k => $v)
        {
            foreach($v as $k1 => $v1)
            {
                $catethree[] = DB::table('category')
                            -> where('pid', $v1 -> id)
                            -> get();
            }
        }

        // dd($data);
        return view('home.search.list',['data' => $data,'series' => $request -> series,'cateone' => $cateone,'catetwo' => $catetwo,'catethree' => $catethree]);

    }
}
